﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsWarning : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsWarning()
        {
            InitializeComponent();
        }
        Operator op = new Operator();
      
    

   

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                textBoxPYM.ReadOnly = true;
                textBoxYPID.ReadOnly = true;
                textBoxYPMC.ReadOnly = true;
                textBoxJLDW.ReadOnly = true;
                textBoxXYKC.ReadOnly = true;
                textBoxBJZSX.ReadOnly = true;
                textBoxBJZXX.ReadOnly = true;
                DataTable dt_warn = new DataTable();
                dt_warn = op.getDataTable("select * from drugsWarning where drugsId='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "drugsWarning");

                this.textBoxSetPYM.Text = "";
                this.textBoxSetYPID.Text = ""; 
                this.textBoxYPID.Text = dt_warn.Rows[0]["drugsId"].ToString();
                this.textBoxYPMC.Text = dt_warn.Rows[0]["drugsName"].ToString();
                this.textBoxPYM.Text = dt_warn.Rows[0]["drugsPY"].ToString();
                this.textBoxJLDW.Text = dt_warn.Rows[0]["drugsUnit"].ToString();
                this.textBoxXYKC.Text = dt_warn.Rows[0]["drugsNum"].ToString();
                this.textBoxBJZSX.Text = dt_warn.Rows[0]["drugsMax"].ToString();
                this.textBoxBJZXX.Text = dt_warn.Rows[0]["drugsMin"].ToString();

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void buttonXG_Click(object sender, EventArgs e)
        {
            textBoxSetPYM.ReadOnly = true;
            textBoxSetYPID.ReadOnly = true;
            textBoxYPMC.ReadOnly = true;
            textBoxJLDW.ReadOnly = true;
            textBoxXYKC.ReadOnly = true;
            textBoxBJZSX.ReadOnly = false;
            textBoxBJZXX.ReadOnly = false;

            DataTable dt1 = new DataTable();
            string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning order by drugsId";
            dt1 = op.getDataTable(sql_a, "drugsInfo");
            dataGridView1.DataSource = dt1;
        }

        private void buttonBC_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBoxBJZSX.Text!="")
                {
                    if (textBoxBJZXX.Text != "")
                    {
                        
                        string select_id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//选择的当前行第一列的值，也就是ID
                        string sql_1 = "update drugsWarning set drugsMax ='" + textBoxBJZSX.Text + "',drugsMin ='" + textBoxBJZXX.Text + "' where drugsID='" + select_id + "'";
                        op.ExecuteSql(sql_1);
                        this.textBoxSetYPID.Text = "";
                        this.textBoxSetPYM.Text = "";
                        this.textBoxYPMC.Text = "";
                        this.textBoxPYM.Text = "";
                        this.textBoxYPID.Text = "";
                        this.textBoxJLDW.Text = "";
                        this.textBoxXYKC.Text = "";
                        this.textBoxBJZSX.Text = "";
                        this.textBoxBJZXX.Text = "";

                        DataTable dt2 = new DataTable();
                        string sql_c = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning order by drugsId";
                        dt2 = op.getDataTable(sql_c, "drugsWarning");
                        dataGridView1.DataSource = dt2;

                        textBoxSetPYM.ReadOnly = false;
                        textBoxSetYPID.ReadOnly = false;
                        textBoxYPID.ReadOnly = true;
                        textBoxPYM.ReadOnly = true;
                        textBoxYPMC.ReadOnly = true;
                        textBoxJLDW.ReadOnly = true;
                        textBoxXYKC.ReadOnly = true;
                        textBoxBJZSX.ReadOnly = true;
                        textBoxBJZXX.ReadOnly = true;
                    }
                    else { MessageBox.Show("药品下限值不能为空！");}
                }
                else { MessageBox.Show("药品上限值不能为空！"); }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void buttonTC_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void buttonSX_Click(object sender, EventArgs e)
        {
            try
            {
                this.textBoxSetYPID.Text = "";
                this.textBoxSetPYM.Text = "";
                this.textBoxYPMC.Text = "";
                this.textBoxYPID.Text = "";
                this.textBoxPYM.Text = "";
                this.textBoxJLDW.Text = "";
                this.textBoxXYKC.Text = "";
                this.textBoxBJZSX.Text = "";
                this.textBoxBJZXX.Text = "";
                DataTable dt1 = new DataTable();
                string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning order by drugsId";
                dt1 = op.getDataTable(sql_a, "drugsInfo");
                dataGridView1.DataSource = dt1;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void FrmDrugsWarning_Load(object sender, EventArgs e)
        {
            DataTable dt1 = new DataTable();
            string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning order by drugsId";
            dt1 = op.getDataTable(sql_a, "drugsInfo");
            dataGridView1.DataSource = dt1;
        }

        private void buttonQBYP_Click(object sender, EventArgs e)
        {
            try
            {
                this.textBoxSetYPID.Text = "";
                this.textBoxSetPYM.Text = "";
                this.textBoxYPMC.Text = "";
                this.textBoxYPID.Text = "";
                this.textBoxPYM.Text = "";
                this.textBoxJLDW.Text = "";
                this.textBoxXYKC.Text = "";
                this.textBoxBJZSX.Text = "";
                this.textBoxBJZXX.Text = "";
                DataTable dt1 = new DataTable();
                string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning order by drugsId";
                dt1 = op.getDataTable(sql_a, "drugsInfo");
                dataGridView1.DataSource = dt1;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void textBoxSetPYM_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt2 = new DataTable();
                string sql_b = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning  where drugsPY like '%" + textBoxSetPYM.Text + "%'";
                dt2 = op.getDataTable(sql_b, "drugsWarning");
                dataGridView1.DataSource = dt2;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void textBoxSetYPID_TextChanged(object sender, EventArgs e)
        {
             try
            {
                 DataTable dt1 = new DataTable();
                 string sql_a = "select drugsId 药品ID,drugsName 药品名称, drugsPY 拼音码, drugsUnit 计量单位, drugsNum 库存数量,drugsMax 报警值上限,drugsMin 报警值下限 from drugsWarning  where drugsID like '%" + textBoxSetYPID.Text + "%'";
                 dt1 = op.getDataTable(sql_a, "drugsWarning");
                 dataGridView1.DataSource = dt1;
            }
             catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }


    }
}